let editingId = null;
let requestUrl = null;

function addHandler() {
    requestUrl = "/potency_fruit_pattern/insert";

    $("#potency_dialog").dialog("open");
}

function editHandler() {
    let data = $("#potency_list").datagrid("getSelected");

    if(data) {
        requestUrl = "/potency_fruit_pattern/updateById";

        $("#id").val(data.id);
        $("#roleId").combobox("setValue", data.roleId);
        $("#schemaId").combobox("setValue", data.schemaId);
        $("#patternId").combobox("setValue", data.patternId);

        $("#potency_dialog").dialog("open");
    } else {
        alertMsg("请选择要修改的记录！", "info");
    }
}

function deleteHandler() {
    let rowData = $("#potency_list").datagrid("getSelected");

    if(rowData) {
        $.messager.confirm("系统提示", "是否确认删除？", function(bool) {
            if (bool) {
                ajaxPost("/potency_fruit_pattern/deleteById", {
                    id: rowData.id
                }, function(resp) {
                    showMsg(resp.message);

                    $("#potency_list").datagrid("reload");
                }, error);
            }
        });
    } else {
        unselected("请选择要删除的记录！");
    }
}

$(document).ready(function () {

    $("#role_id").combobox({
        url: "/role_account/selectAll",
        width: 150,
        valueField: "id",
        textField: "name",
        panelHeight: "auto",
        prompt: "-选择角色-",
        formatter: function(row) {
            ajaxGet("/role_category/selectById", {
                id: row.categoryId
            }, function(resp) {
                let data = resp.data;

                $("#role_modeling_" + row.id).attr({
                    title: data.name,
                    src: data.image,
                    alt: data.name
                });
            }, error);

            return "<img height='20' id='role_modeling_" + row.id + "' />&nbsp;" + row.name;
        }
    });

    $("#pattern_id").combobox({
        url: "/potency_fruit_pattern_category/selectAll",
        width: 150,
        valueField: "id",
        textField: "name",
        groupField: "type",
        prompt: "-选择图案-",
        panelHeight: "auto",
        groupFormatter: function(group) {
            ajaxGet("/potency_fruit_pattern_type/selectById", {
                id: group
            }, function (resp) {
                let data = resp.data;

                $("#type_" + group).html(data.name);
            }, error);

            return "<span id='type_" + group + "'></span>";
        },
        formatter: function(row) {
            return "<img height='20' src='" + row.image + "' />&nbsp;" + row.name;
        }
    });

    $("#search").linkbutton({
        iconCls: "icon-search"
    }).click(function() {
        let patternId = $("#pattern_id").combobox("getValue");
        let roleId = $("#role_id").combobox("getValue");

        $("#potency_list").datagrid("reload", {
            patternId: patternId,
            roleId: roleId
        });
    });

    $("#clear").linkbutton({
        iconCls: "icon-delete"
    }).click(function () {
        $("#search_form").form("clear");
    });

    /******************************************************************************************************/

    $("#patternId").combobox({
        url: "/potency_fruit_pattern_category/selectAll",
        valueField: "id",
        textField: "name",
        width: 150,
        required: true,
        groupField: "type",
        panelHeight: "auto",
        groupFormatter: function(group) {
            ajaxGet("/potency_fruit_pattern_type/selectById", {
                id: group
            }, function (resp) {
                let data = resp.data;

                $("#pattern_type_" + group).html(data.name);
            }, error);

            return "<span id='pattern_type_" + group + "'></span>";
        },
        formatter: function(row) {
            return "<img height='20' src='" + row.image + "' />&nbsp;" + row.name;
        }
    });

    $("#schemaId").combobox({
        url: "/potency_fruit_schema/selectByRoleId",
        valueField: "id",
        textField: "name",
        width: 150,
        required: true,
        panelHeight: "auto"
    });

    $("#roleId").combobox({
        url: "/role_account/selectAll",
        valueField: "id",
        textField: "name",
        width: 150,
        required: true,
        panelHeight: "auto",
        formatter: function(row) {
            ajaxGet("/role_category/selectById", {
                id: row.categoryId
            }, function(resp) {
                let data = resp.data;

                $("#role_category_" + row.id).attr({
                    title: data.name,
                    src: data.image,
                    alt: data.name
                });
            }, error);

            return "<img height='18' id='role_category_" + row.id + "' />&nbsp;" + row.name;
        },
        onSelect: function (record) {
            $("#schemaId").combobox("reload", {
                roleId: record.id
            });
        }
    });

    $("#potency_dialog").dialog({
        title: "角色潜能",
        width: 440,
        buttons: [{
            iconCls: "icon-save",
            text: "保存",
            handler: function() {
                let selector = "#potency_form";

                checkForm(selector, function () {
                    let data = $(selector).serialize();

                    ajaxPost(requestUrl, data, function(resp) {
                        showMsg(resp.message);

                        $(selector).form("clear");
                        $("#potency_dialog").dialog("close");
                        $("#potency_list").datagrid("reload");
                    }, error);
                });
            }
        }, {
            iconCls: "icon-cancel",
            text: "取消",
            handler: function() {
                $("#potency_dialog").dialog("close");
                $("#potency_form").form("clear");
            }
        }]
    });

    /******************************************************************************************************/

    /**
     * 表格体的高度
     * @type {number}
     */
    let height_body = heigt_tr_with_role_details * 10;
    /**
     * 表格高度
     * @type {number}
     */
    let height_table = heigt_tool + heigt_header + height_body + heigt_footer + heigt_scoll_bar + 2;

    $("#potency_list").datagrid({
        url: "/potency_fruit_pattern/selectByPage",
        method: "get",
        height: height_table,
        fitColumns: true,
        rownumbers: true,
        pagination: true,
        toolbar: [{
            iconCls: "icon-add",
            text: "添加",
            handler: function() {
                addHandler();
            }
        }, "-", {
            iconCls: "icon-edit",
            text: "修改",
            handler: function() {
                editHandler();
            }
        }],
        columns: [[
            {field: "id", hidden: true},
            {field: "schemaId", title: "潜能果方案", align: "center", width: 160
                , formatter: function(value, rowData, rowIndex) {
                    ajaxGet("/potency_fruit_schema/selectById", {
                        id: value
                    }, function(resp) {
                        let data = resp.data;

                        $("#schema_" + rowIndex).html(data.name);
                    }, error);

                    return "<div id='schema_" + rowIndex + "'></div>";
                }
            },
            {field: "patternId", title: "潜能果图案", align: "center", width: 160
                , formatter: function(value, rowData, rowIndex) {
                    ajaxGet("/potency_fruit_pattern_category/selectById", {
                        id: value
                    }, function(resp) {
                        let data = resp.data;

                        $("#potency_" + rowIndex).attr({
                            src: data.image,
                            title: data.name
                        });
                    }, error);

                    return "<img height='" + size + "' id='potency_" + rowIndex + "' />";
                }
            },
            roleDetailsColumn
        ]]
    });

});